package com.xy.iot.serial.port.handler;

import com.xy.iot.serial.port.listener.MySerialPortEventListener;
import com.xy.iot.serial.port.listener.SerialPortHandler;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
/**
 *  类功能说明：netty接收串口数据进行拆包处理,目前这个解码器是乱写，如果要用，需要根据当时的串口协议进行修改
 */
@Slf4j
public class NettyRxtxMessageDecoder extends MessageToMessageDecoder<ByteBuf> {

	@Override
	protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
		try {
			byte[] msg = new byte[in.readableBytes()];
			in.readBytes(msg);
			SerialPortHandler serialPortHandler= new MySerialPortEventListener();
			serialPortHandler.handler(msg);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			throw e;
		}
	}
}
